﻿#include "guild_members_logic.h"

#include "GuildMember_struct.h"
#include "GuildModuleMembers_struct.h"
#include "error/make_error_code.h"
#include "guild/guild_loader.hpp"
#include "guild/module/guild_module_loader.hpp"
#include "guild/module/guild_module_saver.hpp"
#include "guild/module/guild_module_tool.hpp"
#include "util/time_util.hh"

namespace guild_service {

bool GuildMembersLogic::GetGuildWelfareAward(
    const role_uuid_type role_uuid, error_code &ec) {
  if (role_uuid == kInvalidRoleUuid) {
    ec = ErrorCode::ERR_INVALID_ROLE_UUID;
    return false;
  }
  guild_id_type guild_id = kInvalidGuildId;
  if (!detail::LoadGuildIdByRole(kGuildServiceWelfareName, role_uuid,
                                 guild_id)) {
    ec = ErrorCode::ERR_GUILD_NOT_EXIST;
    return false;
  }

  GuildModuleMembers module_members;
  if (!detail::GetGuildModule<GuildModuleMembers>(
      kGuildServiceWelfareName, guild_id, kGuildModuleMembersName, module_members)) {
    ec = ErrorCode::ERR_GUILD_MEMBERS_LOAD_FAILED;
    return false;
  }
  for (const auto &it : module_members.members) {
    if (it.role_uuid == role_uuid) {
      if (it.is_weekly_welfare_awarded) {
        ec = ErrorCode::ERR_GUILD_MEMBERS_WELFARE_AWARDED;
        return false;
      }
      break;
    }
  }
  uint64_t welfare_award = 0;
  if (!detail::GuildModuleTool::CalMemberWelfareAward(
          kGuildServiceWelfareName, guild_id, role_uuid, welfare_award)) {
    return false;
  }
  {
    // TODO 发奖
  }

  // ec = ErrorCode::ERR_GUILD_MEMBERS_ROLE_NOT_EXIST;
  return true;
}

}  // namespace guild_service
